﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using System.ComponentModel;
using CCH.Entity;

namespace CCH.DAO
{
    [DataObject(true)]
    public class sysfunctionDAO
    {
        private Entities model = new Entities();

        /// <summary>
        /// 取得第一層
        /// </summary>
        /// <returns></returns>
        public IQueryable<sysfunction> Get_sysfunction_parent()
        {
            return from d in model.sysfunction where d.sfu_parent == 0 orderby d.sfu_order select d;
        }

        /// <summary>
        /// 取得第二層
        /// </summary>
        /// <param name="sfu_parent"></param>
        /// <returns></returns>
        public IQueryable<sysfunction> Get_sysfunction(int sfu_parent)
        {
            return from d in model.sysfunction where d.sfu_parent == sfu_parent orderby d.sfu_order select d;
        }

        /// <summary>
        /// 取得父代編號
        /// </summary>
        /// <param name="sfu_no"></param>
        /// <returns></returns>
        public int Get_sfu_parentNO(int sfu_no)
        {
            var data = (from d in model.sysfunction where d.sfu_no == sfu_no
                        from s in model.sysfunction where s.sfu_no == d.sfu_parent
                        select s.sfu_no).FirstOrDefault();
            return data;
        }

        /// <summary>
        /// 取得第一順位的子代
        /// </summary>
        /// <param name="sfu_parent"></param>
        /// <returns></returns>
        public sysfunction Get_first_sfuNO(int sfu_parent)
        {
            var data = (from d in model.sysfunction
                        where d.sfu_parent == sfu_parent
                        orderby d.sfu_order
                        select d).FirstOrDefault();
            return data;
        }

        public string Get_titleName(int sfu_no)
        {
            var data = (from d in model.sysfunction where d.sfu_no == sfu_no select d).FirstOrDefault();
            var parent = (from d in model.sysfunction where d.sfu_no == data.sfu_parent select d).FirstOrDefault();

            if (data != null && parent != null)
            {
                return string.Format("{0} / {1}", parent.sfu_name, data.sfu_name);
            }
            else
            {
                return "";
            }
            
        }

        public sysfunction get_sysfunctionData(int sfu_no)
        {
            return (from d in model.sysfunction where d.sfu_no == sfu_no select d).FirstOrDefault();
        }

        public int Get_pathNO(int sfu_pathno, string sfu_type)
        {
            return (from d in model.sysfunction
                    where d.sfu_pathNO == sfu_pathno && d.sfu_type == sfu_type
                    select d.sfu_no).First();
        }
    }
}
